package top.ddandang.blog.service.impl;
import	java.util.ArrayList;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import top.ddandang.blog.model.pojo.ArticleTags;
import top.ddandang.blog.mapper.ArticleTagsMapper;
import top.ddandang.blog.service.ArticleTagsService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author D
 * @since 2020-07-07
 */
@Service
@Slf4j
public class ArticleTagsServiceImpl extends ServiceImpl<ArticleTagsMapper, ArticleTags> implements ArticleTagsService {

    @Resource
    private ArticleTagsMapper articleTagsMapper;

    @Async
    @Override
    public void insertArticleTags(Integer[] tagsId, Integer articleId) {

        for (int id : tagsId) {
            ArticleTags articleTags = new ArticleTags()
                    .setTagsId(id)
                    .setArticleId(articleId);
            while (true) {
                if(articleTagsMapper.insert(articleTags) == 1) {
                    break;
                }
            }
        }
    }

    @Override
    public int removeArticleTags(Integer articleId, Integer tagsId) {

        QueryWrapper<ArticleTags> queryWrapper = new QueryWrapper<ArticleTags>()
                .eq("tags_id", tagsId)
                .eq("article_id", articleId);
        return articleTagsMapper.delete(queryWrapper);
    }

    @Override
    public Map<Integer, Integer> addArticleTags(Integer[] articleId, Integer[] tagsId) {
        //tagsId 数组转List<Integer>
        // 直接进行转换然后操作remove 等操作的时候会报java.lang.UnsupportedOperationException异常
        //List<Integer> tagsIdList = Arrays.asList(tagsId);
        //解决方案
        //List<Integer> tagsIdList = new ArrayList<> (Arrays.asList(tagsId));

        //用于统计标签的次数
        Map<Integer, Integer> tagsCount = new HashMap<> ();
        //初始化次数 次数为文章Id的长度
        for(Integer key : tagsId) {
            tagsCount.put(key, articleId.length);
        }
        for (Integer article : articleId) {
            //数组转List<Integer>
            List<Integer> tagsIdList = new ArrayList<> (Arrays.asList(tagsId));
            Map<String, Object> map = new HashMap<>();
            map.put("articleId", article);
            map.put("tagsId", tagsId);
            //查询数据库改文章是否存在这些标签，返回存在的标签然后从数组中删除
            List<Integer> tags = articleTagsMapper.listArticleTags(map);
            //将已经存在的标签Id进行删除
            if(tags.size() != 0) {
                for (Integer tag : tags) {
                    //如果包含这个标签则删除 不会重复添加标签
                    //并且给标签的次数减一
                    if(tagsIdList.contains(tag)) {
                        tagsIdList.remove(tag);
                        tagsCount.put(tag, tagsCount.get(tag)-1);
                    }

                }
            }
            //集合转数组
            //Integer[] newTagsId = tagsIdList.toArray(new Integer[0]);
            //增加标签
            for (Integer newTagsId : tagsIdList) {
                ArticleTags articleTags = new ArticleTags()
                        .setArticleId(article)
                        .setTagsId(newTagsId);
                articleTagsMapper.insert(articleTags);
            }

        }
        return tagsCount;
    }
}
